<?php

namespace App\Admin\Controllers;

use App\Models\Task;
use App\Models\TaskType;
use App\Models\TaskStep;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Row;
use Encore\Admin\Widgets\Box;
use Encore\Admin\Widgets\InfoBox;
use Encore\Admin\Widgets\Table;
use Illuminate\Http\Request;

class TaskController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('任务')
            ->description('任务列表')
            ->row(function (Row $row) {
                $row->column(3, function (Column $column) {
                    $total = Task::count();
                    $infoBox = new InfoBox('任务总数', 'tasks', 'aqua', '/admin/task', $total);
                    $column->append($infoBox);
                });
                $row->column(3, function (Column $column) {
                    $today_total = Task::whereDate('created_at', date('Y-m-d'))->count();
                    $infoBox = new InfoBox('今日新增任务', 'tasks', 'aqua', '', $today_total);
                    $column->append($infoBox);
                });

            })
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('详情')
            ->description('任务详情')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('编辑')
            ->description('任务编辑')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('新建')
            ->description('新建任务')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Task);
        $grid->model()->orderBy('id', 'desc');
        $grid->id('Id');
        $grid->name('任务名称');
        $grid->type_id('任务类型')->display(function(){
            $data = TaskType::where('id',$this->type_id)->first();
            if($data){
                return $data->name;
            }
        });
        $grid->total_count('任务总量')->sortable();
        $grid->money('每单佣金')->sortable();
        $grid->s_time('开始时间')->sortable();
        $grid->e_time('结束时间')->sortable();
        $grid->dead_line('结算天数')->sortable();
        $grid->task_detail('任务描述');
        $grid->created_at('创建时间');
        $grid->updated_at('更新时间');

        $grid->actions(function ($actions) {
            $actions->disableView();
            // append一个操作
            // $actions->append("<a href='/admin/task_step/" . $actions->getKey() . "'>" . '<i class="fa fa-sort-amount-asc"></i>' . "任务流程</a>");
            $actions->append("<a href='/admin/taskstep?&task_id=" . $actions->getKey() . "'>" . '<i class="fa fa-sort-amount-asc"></i>' . "任务流程</a>");
            $actions->append("<a href='/admin/tasksubmit?&task_id=" . $actions->getKey() . "'>" . '<i class="fa fa-sort-amount-asc"></i>' . "任务提交</a>");

        });
        $grid->filter(function ($filter) {
            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            $filter->equal('type_id','任务类型')->select('/admin/get_type_list');
            $filter->where(function ($query) {
                $query->whereHas('task', function ($query) {
                    $query->where('name', 'like', "%{$this->input}%");
                });
            }, '任务名称');
            $filter->between('created_at', '创建时间')->datetime();
        });
        return $grid;
    }
    //步骤详情
    public function task_step(Request $request,$id)
    {   
        $tasksteps = TaskStep::where('task_id',$id)->get();
        
        $headers = ['ID','任务名称', '第几步', '步骤描述','步骤图片','操作'];
        $ret = [];
        foreach ($tasksteps as $v) {
            $taskname = Task::where('id',$v->task_id)->value('name');
            $step_img_arr = $v->step_img;
            $img_count = count($step_img_arr);
            switch ($img_count) {
                case '1':
                    $ret[] = [$v->id, $taskname, $v->step, $v->step_detail,"<img src='/uploads/".$step_img_arr[0]." ' style='max-width:200px;max-height:200px' class='img img-thumbnail'>","<a href='/admin/taskstep/" . $v->id . "/edit'>" . '<i class="fa fa-edit"></i>' . "编辑</a>"];
                    break;
                case '2':
                    $ret[] = [$v->id, $taskname, $v->step, $v->step_detail,"<img src='/uploads/".$step_img_arr[0]." ' style='max-width:200px;max-height:200px' class='img img-thumbnail'><img src='/uploads/".$step_img_arr[1]." ' style='max-width:200px;max-height:200px' class='img img-thumbnail'>","<a href='/admin/taskstep/" . $v->id . "/edit'>" . '<i class="fa fa-edit"></i>' . "编辑</a>"];
                    break;
                case '3':
                    $ret[] = [$v->id, $taskname, $v->step, $v->step_detail,"<img src='/uploads/".$step_img_arr[0]." ' style='max-width:200px;max-height:200px' class='img img-thumbnail'><img src='/uploads/".$step_img_arr[1]." ' style='max-width:200px;max-height:200px' class='img img-thumbnail'><img src='/uploads/".$step_img_arr[0]." ' style='max-width:200px;max-height:200px' class='img img-thumbnail'>","<a href='/admin/taskstep/" . $v->id . "/edit'>" . '<i class="fa fa-edit"></i>' . "编辑</a>"];
                    break;               
                default:
                    # code...
                    break;
            }           
        }
        $table = new Table($headers, $ret);

        $box = new Box('步骤详情', $table->render());
        $box->style('primary');
        $box->solid();

        $content = new Content();
        return $content
            ->header('任务步骤')
            ->description('列表')
            ->body($box->render());
    }
    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Task::findOrFail($id));

        $show->id('Id');
        $show->name('任务名');
        $show->type('Type_id');
        $show->total_count('任务总数目');
        $show->money('单位佣金');
        $show->s_time('S time');
        $show->e_time('E time');
        $show->dead_line('Dead line');
        $show->task_detail('Task detail');
        $show->created_at('Created at');
        $show->updated_at('Updated at');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Task);

        $form->text('name', 'Name');
        $form->select('type_id', '任务类型')->options('/admin/get_type_list');
        $form->text('total_count', '任务总量');
        $form->decimal('money', '佣金');
        $form->datetime('s_time', '开始时间')->default(date('Y-m-d'));
        $form->datetime('e_time', '结束时间')->default(date('Y-m-d'));
        $form->number('dead_line', '结算天数');
        $form->textarea('task_detail', '任务描述');

        return $form;
    }
    //获取任务列表
    public function get_task_list()
    {
        $data = Task::get(['id','name as text']);
        return $data->toArray();
    }
}
